﻿using GalaSoft.MvvmLight;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using ASoft.Msg;

namespace ASoft.WPF.ViewModel
{

    public class WeatherStationViewModel : ViewModelBase
    {

        public WeatherStationViewModel()
        {
            this.气象站设备信息 = new WeatherStationSetMsgViewModel();
            this.气象站模似信息 = new WeatherStationReturnMsgBaseViewModel();
            this.偏移量 = new RootDataViewModel();

            if (IsInDesignMode)
            {
                // Code runs in Blend --> create design time data.
            }
            else
            {
                this.启用模似气象站 = RC.Current.RootData.启用模似气象站;
                WPFUtility.TransFromDataToViewModel(RC.Current.RootData.气象站模似信息, this.气象站模似信息);
                WPFUtility.TransFromDataToViewModel(RC.Current.RootData.气象站设备信息, this.气象站设备信息);
                WPFUtility.TransFromDataToViewModel(RC.Current.RootData, this.偏移量);
            }

        }

        public WeatherStationSetMsgViewModel 气象站设备信息 { get; private set; }

        public WeatherStationReturnMsgBaseViewModel 气象站模似信息 { get; private set; }

        public RootDataViewModel 偏移量 { get; private set; }


        #region 启用模似气象站

        /// <summary>
        /// The <see cref="启用模似气象站" /> property's name.
        /// </summary>
        public const string 启用模似气象站PropertyName = "启用模似气象站";

        private bool _启用模似气象站 = false;


        public bool 启用模似气象站
        {
            get
            {
                return _启用模似气象站;
            }

            set
            {
                if (_启用模似气象站 == value)
                {
                    return;
                }

                var oldValue = _启用模似气象站;
                _启用模似气象站 = value;

                RaisePropertyChanged(启用模似气象站PropertyName);

            }
        }

        #endregion

        #region "TagString"

        private string _TagString = default(string);

        public string TagString
        {
            get
            {
                return _TagString;
            }
            set
            {
                if (_TagString == value)
                {
                    return;
                }

                var oldValue = _TagString;
                _TagString = value;

                RaisePropertyChanged("TagString");
            }
        }

        #endregion

        #region 命令

        public ICommand 气象站设置数据写设备命令
        {
            get
            {
                return new RelayCommand(
                      () =>
                      {
                          WPFUtility.TransFromViewModelToData(this.气象站设备信息, RC.Current.RootData.气象站设备信息);
                          var returnMsg = RC.Current.MsgService.TransSendMsg(RC.Current.RootData.气象站设备信息)
                              as WeatherStationReturnSetMsg;
                          RC.Current.SaveRootData();


                          if (returnMsg == null || returnMsg.状态 == 0)
                          {
                              TagString = "写设备失败";
                              return;
                          }
                          TagString = "写设备成功";

                      },
                      () =>
                      {
                          if (IsInDesignMode)
                          {
                              return false;
                          }
                          return true;
                      });
            }
        }

        public ICommand 气象站数据查询命令
        {
            get
            {
                return new RelayCommand(
                      () =>
                      {
                          var msg = new WeatherStationSearchMsg();
                          var returnMsg = RC.Current.MsgService.TransSendMsg(msg) as WeatherStationReturnSearchMsg;
                          if (returnMsg == null)
                          {
                              TagString = "查询失败";
                              return;
                          }
                          RC.Current.气象站实时数据 = returnMsg;
                          RC.Current.气象站实时数据来源方式 = EWeatherStationDataStyle.上位机查询;
                          TagString = "查询成功";

                      },
                      () =>
                      {
                          if (IsInDesignMode)
                          {
                              return false;
                          }
                          return true;
                      });
            }
        }

        public ICommand 气象站模似数据保存命令
        {
            get
            {
                return new RelayCommand(
                      () =>
                      {
                          WPFUtility.TransFromViewModelToData(this.气象站模似信息, RC.Current.RootData.气象站模似信息);
                          RC.Current.RootData.启用模似气象站 = this.启用模似气象站;
                          (new ViewModelLocator()).Main.气象数据.检测结果 =
                              this.启用模似气象站 ? ECheckFlag.模似 : ECheckFlag.未检测;
                          RC.Current.SaveRootData();
                          TagString = "气象站模似数据保存成功";
                          RC.Current.RootData.SetObserversData(Data.EObserverType.气象站实时信息);
                      },
                      () =>
                      {
                          if (IsInDesignMode)
                          {
                              return false;
                          }
                          if (this.启用模似气象站 != RC.Current.RootData.启用模似气象站)
                          {
                              return true;
                          }
                          //return !WPFUtility.DataEqualsViewModel(RC.Current.RootData.气象站模似信息, this.气象站模似信息);
                          return true;
                      });
            }
        }

        #endregion

        public ICommand 气象站偏移量保存命令
        {
            get
            {
                return new RelayCommand(
                      () =>
                      {
                          WPFUtility.TransFromViewModelToData(this.偏移量, RC.Current.RootData);
                          RC.Current.SaveRootData();
                          TagString = "气象站偏移量保存成功";
                      },
                      () =>
                      {
                          if (IsInDesignMode)
                          {
                              return false;
                          }

                          //return !WPFUtility.DataEqualsViewModel(RC.Current.RootData, this.偏移量);
                          return true;
                      });
            }
        }

    }
}